//
//  UIImage+CXImage.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/27.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "UIImage+CXImage.h"

@implementation UIImage (CXImage)

-(instancetype) cx_circleImage{
    
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
    //描述裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.size.width, self.size.width)];
    //设置裁剪区域
    [path addClip];
    //画图形
    [self drawAtPoint:CGPointZero];
    //取出图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return image;
}



+(instancetype)cx_circleWithImage:(NSString *)imageName{
    
    return [[self imageNamed:imageName] cx_circleImage];
}


+(UIImage *)imageOriginalWith:(NSString *)imageName{
    
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}


+(UIImage *)resizeableImageWithLocalImageName:(NSString *)localImageName{
    UIImage *image = kGetImage(@"nav_bg_white");
    //获取图片的尺寸
    CGFloat imageW = image.size.width;
    CGFloat imageH = image.size.height;
    return [image stretchableImageWithLeftCapWidth:imageW * 0.5 topCapHeight:imageH * 0.5];
}


+ (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
}


@end
